package br.com.nooclix.salon.controller;

import br.com.nooclix.salon.application.ProductApplication;
import br.com.nooclix.salon.controller.util.ResponseData;
import br.com.nooclix.salon.domain.product.Product;
import br.com.nooclix.salon.infrastructure.util.ApplicationContextRegister;

public class ProductController {
	private ProductApplication productApplication = ApplicationContextRegister
			.getBean("productApplication", ProductApplication.class);

	public ResponseData persist(Product product) {
		ResponseData responseData = new ResponseData();
		try {
			// Caso não setar o setCode como null, dará conflito com o banco
			product.setCode(null);
			productApplication.persist(product);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
	}

	public ResponseData update(Product product) {
		ResponseData responseData = new ResponseData();

		try {

			productApplication.update(product);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;
	}

	public ResponseData remove(Product product) {
		ResponseData responseData = new ResponseData();

		try {

			// configurando para ser NULL por causa do FLEX ter padrao 0(ZERO)

			productApplication.delete(product);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

	public ResponseData findByName(String name) {
		ResponseData responseData = new ResponseData();

		try {
			
			responseData.setItems(productApplication.findByName(name));
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

}
